<?php

require_once __DIR__ . '/../vendor/autoload.php';

use WebmanGrpc\Grpc;
use WebmanGrpc\Plugin;

// 初始化配置
$config = [
    'enable' => true,
    'grpc' => [
        'enabled' => true,
        'server' => [
            'host' => '0.0.0.0',
            'port' => 50051,
            'credentials' => null,
            'max_recv_msg_length' => 4194304,
            'max_send_msg_length' => 4194304,
        ],
        'client' => [
            'timeout' => 30,
            'credentials' => null,
            'max_recv_msg_length' => 4194304,
            'max_send_msg_length' => 4194304,
            'retry' => 3,
            'backoff' => [
                'initial' => 100,
                'max' => 1000,
                'multiplier' => 2,
            ],
            'connection_pool' => [
                'max_connections' => 10,
                'idle_timeout' => 60,
                'max_lifetime' => 300,
            ],
        ],
        'services' => [
            // 示例服务配置
            // 'helloworld.Greeter' => [
            //     'class' => \App\Service\GreeterService::class,
            //     'options' => [],
            // ],
        ],
        'interceptors' => [
            [
                'class' => \WebmanGrpc\Interceptor\LoggingInterceptor::class,
                'priority' => 100,
            ],
        ],
    ],
    'http_gateway' => [
        'enabled' => true,
        'routes' => [
            // 示例路由配置
            // [
            //     'path' => '/api/hello',
            //     'method' => 'POST',
            //     'service' => 'helloworld.Greeter',
            //     'method' => 'SayHello',
            //     'request_transformer' => \App\Transformer\HelloRequestTransformer::class,
            //     'response_transformer' => \App\Transformer\HelloResponseTransformer::class,
            //     'address' => '127.0.0.1:50051',
            //     'options' => [],
            // ],
        ],
    ],
];

// 模拟Webman环境
if (!class_exists('support\Bootstrap')) {
    class_alias('WebmanGrpc\Plugin', 'support\Bootstrap');
}

if (!function_exists('config')) {
    function config($key = null, $default = null) {
        global $config;
        
        if ($key === null) {
            return $config;
        }
        
        $keys = explode('.', $key);
        $value = $config;
        
        foreach ($keys as $k) {
            if (!is_array($value) || !array_key_exists($k, $value)) {
                return $default;
            }
            $value = $value[$k];
        }
        
        return $value;
    }
}

// 模拟Workerman Worker类
if (!class_exists('Workerman\Worker')) {
    class Worker {
        public $listen;
        public $count;
        
        public function __construct($socket_name = '', $context_option = array()) {
            $this->listen = $socket_name;
        }
    }
}

// 模拟Route类
if (!class_exists('Webman\Route')) {
    class Route {
        public static function add($method, $path, $callback) {
            echo "Route registered: {$method} {$path}\n";
        }
    }
}

// 模拟Config类
if (!class_exists('Webman\Config')) {
    class Config {
        public static function get($key = null, $default = null) {
            return config($key, $default);
        }
    }
}

// 模拟json函数
if (!function_exists('json')) {
    function json($data, $code = 200) {
        return new class($data, $code) {
            public function __construct($data, $code) {
                $this->data = $data;
                $this->code = $code;
            }
        };
    }
}

// 设置全局配置
$GLOBALS['config'] = $config;

// 启动插件
$worker = new Worker();
Plugin::start($worker);

echo "Webman gRPC plugin initialized\n";